package com.rabbit.topics;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by IntelliJ IDEA.
 * <p>
 * Author: libingyang
 * Date: 2021-03-15 17:00
 */
public class Consumer {


    public static void main(String[] args) throws IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();


        factory.setHost("192.168.100.54");

        factory.setPort(5672);

        factory.setVirtualHost("/");

        factory.setUsername("guest");

        factory.setPassword("guest");

        // 获取连接对象
        Connection connection = factory.newConnection();


        // 获取连接通道
        Channel channel = connection.createChannel();

        String queueName = channel.queueDeclare().getQueue();

        channel.queueBind(queueName, "topics","user.*");

        channel.basicConsume(queueName,false, new DefaultConsumer(channel){

            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                System.out.println(new String(body));

            }
        });

//        channel.close();
//        connection.close();
    }
}
